<html>
<head><meta charset="utf-8"><title>borrows invalidating raw pointers · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html">borrows invalidating raw pointers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="195093081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195093081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Brubeck <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195093081">(Apr 23 2020 at 17:20)</a>:</h4>
<p>I have some questions about when raw pointers are invalidated, based on comments by <span class="user-mention" data-user-id="120791">@RalfJ</span> in <a href="https://github.com/servo/rust-smallvec/pull/213" title="https://github.com/servo/rust-smallvec/pull/213">https://github.com/servo/rust-smallvec/pull/213</a></p>
<p>This extremely minimal case does not cause a MIRI error. Should it? <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f3b2679b6c99a4ce21fc45079b6d2a05" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f3b2679b6c99a4ce21fc45079b6d2a05">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f3b2679b6c99a4ce21fc45079b6d2a05</a></p>



<a name="195094313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195094313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Brubeck <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195094313">(Apr 23 2020 at 17:30)</a>:</h4>
<p>Even this version passes MIRI, so it's not just that the previous version somehow avoids "actually" creating an <code>&amp;mut</code>:<br>
<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e25d3f2a318af941fbdd332d57f9346a" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e25d3f2a318af941fbdd332d57f9346a">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e25d3f2a318af941fbdd332d57f9346a</a></p>



<a name="195096935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195096935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Brubeck <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195096935">(Apr 23 2020 at 17:53)</a>:</h4>
<p>If I'm understanding this behavior right, creating a new Unique borrow  invalidates the previous raw pointer (clearing the borrow stack), but then the cast from <code>&amp;mut</code> to <code>*mut</code> retags this borrow, so the stack is now <code>[(Untagged: SharedReadWrite)]</code> again, and this makes the previously-invalidated raw pointer valid again:</p>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=bae8fa79cc8c5d1023e8cbd11a050ca6" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=bae8fa79cc8c5d1023e8cbd11a050ca6">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=bae8fa79cc8c5d1023e8cbd11a050ca6</a></p>



<a name="195111394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195111394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yato <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195111394">(Apr 23 2020 at 19:54)</a>:</h4>
<p>Yes, that first write to <code>p0</code> should invalidate <code>p1</code>. MIRI doesn't track raw pointers as well as it should, so MIRI doesn't detect these sorts of aliasing bugs yet.</p>



<a name="195111476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195111476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yato <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195111476">(Apr 23 2020 at 19:54)</a>:</h4>
<blockquote>
<p>If I'm understanding this behavior right, creating a new Unique borrow invalidates the previous raw pointer (clearing the borrow stack), but then the cast from &amp;mut to *mut retags this borrow, so the stack is now [(Untagged: SharedReadWrite)] again, and this makes the previously-invalidated raw pointer valid again:</p>
</blockquote>
<p>Yes, all raw pointers have the same tag (which is untagged) right now, so making a new raw pointer to the same location revives old raw pointers incorrectly.</p>



<a name="195112011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195112011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Brubeck <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195112011">(Apr 23 2020 at 19:59)</a>:</h4>
<p>Thanks!</p>



<a name="195191556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195191556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195191556">(Apr 24 2020 at 14:00)</a>:</h4>
<p><span class="user-mention" data-user-id="270999">@Matt Brubeck</span> note however that to match what LLVM does with <code>noalias</code>, we need to eventually also track raw pointers better</p>



<a name="195191565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195191565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195191565">(Apr 24 2020 at 14:00)</a>:</h4>
<p>and then that kind of code would stop working</p>



<a name="195191761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195191761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195191761">(Apr 24 2020 at 14:02)</a>:</h4>
<p>the trouble is that we have to somehow stop tracking around ptr-to-int-casts, otherwise we break code that is definitely correct. <a href="https://bugs.llvm.org/show_bug.cgi?id=35229" title="https://bugs.llvm.org/show_bug.cgi?id=35229">LLVM gets this wrong</a>, though, and it is unclear what the best approach is here ("unclear" as in "this is an area of PL research").</p>



<a name="195194236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows%20invalidating%20raw%20pointers/near/195194236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Brubeck <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers.html#195194236">(Apr 24 2020 at 14:19)</a>:</h4>
<p>Got it, thanks.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>